from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from src.db.mapper import DatabaseMapper
from src.db.models import S3, Files, Modality
from src.db.session import DatabaseSession
from src.kafka.message_structures.radiology_ct.scoliosis import TopicScoliosisMessage


async def check_if_id_exists(id_: str, db_session: AsyncSession) -> bool:
    try:
        result = await db_session.execute(select(Files).where(Files.id == id_))
        file = result.scalars().first()
        if file:
            return True
    except Exception:
        pass
    return False


async def insert_topic_scoliosis_message(
    message: TopicScoliosisMessage, db_client: DatabaseSession
):
    async_session = await db_client.get_session()
    async with async_session:
        mapper = DatabaseMapper(async_session)
        modality = await mapper.create_model(message, Modality)
        s3 = await mapper.create_model(message, S3)
        await mapper.create_model(message, Files, modality_id=modality.id, s3_id=s3.id)
